package dailyTT;

import java.io.File;
import java.io.FileOutputStream;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class TongueTwisterDaily {
	public static void tryATwister() throws Exception{
		String u = TongueTwisterDaily.class.getResource("").getPath();
		
		String gtdPath=u+"TongueTwisterGTD.xml";
		File GTD = new File(gtdPath);
		
		SAXReader reader = new SAXReader();
		Document doc = reader.read(GTD);
		Element root = doc.getRootElement();
		Attribute nextday=root.attribute("nextday");
		int next=Integer.parseInt(nextday.getValue());
		Attribute maxTT=root.attribute("max");
		int max=Integer.parseInt(maxTT.getValue());
		
		String path=u+"TongueTwisters.xml";
		File tongueTwisters = new File(path);

		Document ttsDoc = reader.read(tongueTwisters);
		Element ttsRoot = ttsDoc.getRootElement();
		Node today=ttsRoot.node(next*2-1);
		String tt = today.getText();
		System.out.println(tt);
		
		next++;
		if(next>max)
			next=1;
		nextday.setValue(next+"");
		OutputFormat f = OutputFormat.createPrettyPrint();
		XMLWriter writer = new XMLWriter(new FileOutputStream(GTD),f);
		writer.write(doc);
	}
}
